Socket
Socket
Sign inDemoInstall

@bearei/react-form

Package Overview
Dependencies
5
Maintainers
1
Versions
34
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @bearei/react-form

Base form components that support React and React native


Version published
Weekly downloads
1
Maintainers
1
Created
Weekly downloads
 

Readme

Source

react-form

Base form components that support React and React native

Installation

yarn add @bearei/react-form --save

Parameters

Form
NameTypeRequiredDescription
formFormInstanceForm instance
initialValuesRecord<string, unknown>Initializes the form value
itemsBaseFormItemPropsForm items
onFinish(options: OnFinishOptions) => voidThis function is called when the form is completed
onFinishFailed(options: Errors) => voidThis function is called when the form fails to complete
onValuesChange(changedValues: Record<string, unknown>, values: Record<string, unknown>) => voidThis function is called when the form field value changes
renderMain(props: FormMainProps) => ReactNodeRender the form main
renderContainer(props: FormContainerProps) => ReactNodeRender the form container
Form Item
NameTypeRequiredDescription
namestringForm item field name
labelReactNodeForm item label
noStyleReactNodeWhether the form item is unstyled
initialValueunknownThe initial value of the form item
extraReactNodeAdditional content for the form item
requiredbooleanWhether the form entry is a required field
renderControl(props: ControlProps) => JSX.ElementRender the controlled component
rulesRuleItem[]Validate rules -- RuleItem
validateFirstbooleanWhen a rule fails, do you stop checking the rest of the rules
renderLabel(props: FormItemLabelProps) => ReactNodeRender the form item label
renderExtra(props: FormItemExtraProps) => ReactNodeRender the form item extra
renderMain(props: FormItemMainProps) => ReactNodeRender the form item main
renderContainer(props: FormItemContainerProps) => ReactNodeRender the form item container

Api

Form instance
NameTypeDescription
getFieldEntities(signOut?: boolean) => FieldEntity[]Gets the form field entities
getFieldEntitiesName(names?: string[], signOut?: boolean) => (string)[]Gets the form field entities name
signInField(entity: FieldEntity) => {signOut: () => void}Sign in form field
signOutField(name?: NamePath) => voidSign out form field
setFieldsValue(values?: Record<string, unknown>, options?: {validate?: boolean; response?: boolean}) => voidSet the value of the form fields
getFieldValue(name?: NamePath): unknownGets the value of the form field
setFieldError(error: Errors) => voidError setting form field
getFieldError(name?: NamePath): Errors Gets the form field error
setInitialValues(values?: Record<string, unknown>, init?: boolean) => voidSet the form initialization value
getInitialValues() => Record<string, unknown>Gets the form initialization value
setCallbacks(callbackValues: Callbacks) => voidSets the form callbacks
setFieldTouched(name?: string, touched?: boolean) => voidSets whether the form field is operated on
isFieldTouched(name?: NamePath) => booleanCheck that the form fields have been manipulated
validateField(name?: NamePath): Promise<Errors>Validate form field
resetField(name?: NamePath) => voidReset the form field
submit(skipValidate?: boolean) => voidComplete the form field submission

Use

import React from 'React';
import ReactDOM from 'react-dom';
import form, { FormItem } from '@bearei/react-form';

interface CostInputProps {
  onValueChange?: (value: string) => void;
  value?: unknown;
}

const CostInput: FC<CostInputProps> = ({ value, onValueChange }) => {
  const [inputValue, setInputValue] = useState('');
  const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
    e.preventDefault();
    const inputtedValue = e.currentTarget.value;

    setInputValue(inputtedValue);
    onValueChange?.(inputtedValue);
  };

  useEffect(() => {
    value && value !== inputValue && setInputValue(`${value}`);
  }, [value]);

  return <input value={inputValue} onChange={handleChange} />;
};

const items = [
  {
    label: 'label1',
    name: 'name1',
    renderControl: (props: ControlProps) => <CostInput {...props} />,
  },
  {
    label: 'label2',
    name: 'name2',
    renderControl: (props: ControlProps) => <CostInput {...props} />,
  },
  {
    label: 'label3',
    name: 'name3',
    renderControl: (props: ControlProps) => <CostInput {...props} />,
  },
];

const form = (
  <Form
    items={items}
    renderMain={({ items }) =>
      items?.map((item, index) => (
        <Form.Item
          key={item.name}
          {...item}
          renderMain={({ value, onValueChange, renderControl }) =>
            renderControl?.({ value, onValueChange })
          }
          renderContainer={({ children }) => <div>{children}</div>}
        />
      ))
    }
    renderContainer={({ children }) => <div>{children}</div>}
  />
);

ReactDOM.render(form, container);

Keywords

FAQs

Last updated on 15 Mar 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc